home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
dns
/
resolver.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
14KB
|
601 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
import socket
import sys
import time
import dns.exception as dns
import dns.message as dns
import dns.name as dns
import dns.query as dns
import dns.rcode as dns
import dns.rdataclass as dns
import dns.rdatatype as dns
if sys.platform == 'win32':
import _winreg
class NXDOMAIN(dns.exception.DNSException):
pass
Timeout = dns.exception.Timeout
class NoAnswer(dns.exception.DNSException):
pass
class NoNameservers(dns.exception.DNSException):
pass
class NotAbsolute(dns.exception.DNSException):
pass
class NoRootSOA(dns.exception.DNSException):
pass
class Answer(object):
def __init__(self, qname, rdtype, rdclass, response):
self.qname = qname
self.rdtype = rdtype
self.rdclass = rdclass
self.response = response
min_ttl = -1
rrset = None
for count in xrange(0, 15):
try:
rrset = response.find_rrset(response.answer, qname, rdclass, rdtype)
if min_ttl == -1 or rrset.ttl < min_ttl:
min_ttl = rrset.ttl
continue
except KeyError:
if rdtype != dns.rdatatype.CNAME:
try:
crrset = response.find_rrset(response.answer, qname, rdclass, dns.rdatatype.CNAME)
if min_ttl == -1 or crrset.ttl < min_ttl:
min_ttl = crrset.ttl
for rd in crrset:
qname = rd.target
break
except KeyError:
raise NoAnswer
except:
None<EXCEPTION MATCH>KeyError
None<EXCEPTION MATCH>KeyError
raise NoAnswer
continue
if rrset is None:
raise NoAnswer
self.rrset = rrset
self.expiration = time.time() + min_ttl
def __getattr__(self, attr):
if attr == 'name':
return self.rrset.name
elif attr == 'ttl':
return self.rrset.ttl
elif attr == 'covers':
return self.rrset.covers
elif attr == 'rdclass':
return self.rrset.rdclass
elif attr == 'rdtype':
return self.rrset.rdtype
else:
raise AttributeError, attr
def __len__(self):
return len(self.rrset)
def __iter__(self):
return iter(self.rrset)
def __getitem__(self, i):
return self.rrset[i]
def __delitem__(self, i):
del self.rrset[i]
def __getslice__(self, i, j):
return self.rrset[i:j]
def __delslice__(self, i, j):
del self.rrset[i:j]
class Cache(object):
def __init__(self, cleaning_interval = 300):
self.data = { }
self.cleaning_interval = cleaning_interval
self.next_cleaning = time.time() + self.cleaning_interval
def maybe_clean(self):
now = time.time()
if self.next_cleaning <= now:
keys_to_delete = []
for k, v in self.data.iteritems():
if v.expiration <= now:
keys_to_delete.append(k)
continue
for k in keys_to_delete:
del self.data[k]
now = time.time()
self.next_cleaning = now + self.cleaning_interval
def get(self, key):
self.maybe_clean()
v = self.data.get(key)
if v is None or v.expiration <= time.time():
return None
return v
def put(self, key, value):
self.maybe_clean()
self.data[key] = value
def flush(self, key = None):
if key is not None:
if self.data.has_key(key):
del self.data[key]
else:
self.data = { }
self.next_cleaning = time.time() + self.cleaning_interval
class Resolver(object):
def __init__(self, filename = '/etc/resolv.conf', configure = True):
self.reset()
if configure:
if sys.platform == 'win32':
self.read_registry()
elif filename:
self.read_resolv_conf(filename)
def reset(self):
self.domain = dns.name.Name(dns.name.from_text(socket.gethostname())[1:])
if len(self.domain) == 0:
self.domain = dns.name.root
self.nameservers = []
self.search = []
self.port = 53
self.timeout = 2
self.lifetime = 30
self.keyring = None
self.keyname = None
self.edns = -1
self.ednsflags = 0
self.payload = 0
self.cache = None
def read_resolv_conf(self, f):
if isinstance(f, str) or isinstance(f, unicode):
try:
f = open(f, 'r')
except IOError:
self.nameservers = [
'127.0.0.1']
return None
want_close = True
else:
want_close = False
try:
for l in f:
if len(l) == 0 and l[0] == '#' or l[0] == ';':
continue
tokens = l.split()
if len(tokens) == 0:
continue
if tokens[0] == 'nameserver':
self.nameservers.append(tokens[1])
continue
if tokens[0] == 'domain':
self.domain = dns.name.from_text(tokens[1])
continue
if tokens[0] == 'search':
for suffix in tokens[1:]:
self.search.append(dns.name.from_text(suffix))
finally:
if want_close:
f.close()
if len(self.nameservers) == 0:
self.nameservers.append('127.0.0.1')
def _determine_split_char(self, entry):
if entry.find(' ') >= 0:
split_char = ' '
elif entry.find(',') >= 0:
split_char = ','
else:
split_char = ' '
return split_char
def _config_win32_nameservers(self, nameservers):
nameservers = str(nameservers)
split_char = self._determine_split_char(nameservers)
ns_list = nameservers.split(split_char)
for ns in ns_list:
if ns not in self.nameservers:
self.nameservers.append(ns)
continue
def _config_win32_domain(self, domain):
self.domain = dns.name.from_text(str(domain))
def _config_win32_search(self, search):
search = str(search)
split_char = self._determine_split_char(search)
search_list = search.split(split_char)
for s in search_list:
if s not in self.search:
self.search.append(dns.name.from_text(s))
continue
def _config_win32_fromkey(self, key):
try:
(servers, rtype) = _winreg.QueryValueEx(key, 'NameServer')
except WindowsError:
servers = None
if servers:
self._config_win32_nameservers(servers)
try:
(dom, rtype) = _winreg.QueryValueEx(key, 'Domain')
if dom:
self._config_win32_domain(servers)
except WindowsError:
pass
except:
None<EXCEPTION MATCH>WindowsError
None<EXCEPTION MATCH>WindowsError
try:
(servers, rtype) = _winreg.QueryValueEx(key, 'DhcpNameServer')
except WindowsError:
servers = None
if servers:
self._config_win32_nameservers(servers)
try:
(dom, rtype) = _winreg.QueryValueEx(key, 'DhcpDomain')
if dom:
self._config_win32_domain(servers)
except WindowsError:
pass
except:
None<EXCEPTION MATCH>WindowsError
None<EXCEPTION MATCH>WindowsError
try:
(search, rtype) = _winreg.QueryValueEx(key, 'SearchList')
except WindowsError:
search = None
if search:
self._config_win32_search(search)
def read_registry(self):
lm = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
want_scan = False
try:
tcp_params = _winreg.OpenKey(lm, 'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters')
want_scan = True
except EnvironmentError:
tcp_params = _winreg.OpenKey(lm, 'SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP')
try:
self._config_win32_fromkey(tcp_params)
finally:
tcp_params.Close()
if want_scan:
interfaces = _winreg.OpenKey(lm, 'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces')
try:
i = 0
while True:
try:
guid = _winreg.EnumKey(interfaces, i)
i += 1
key = _winreg.OpenKey(interfaces, guid)
if not self._win32_is_nic_enabled(lm, guid, key):
continue
try:
self._config_win32_fromkey(key)
finally:
key.Close()
continue
except EnvironmentError:
break
continue
None<EXCEPTION MATCH>EnvironmentError
finally:
interfaces.Close()
lm.Close()
def _win32_is_nic_enabled(self, lm, guid, interface_key):
try:
connection_key = _winreg.OpenKey(lm, 'SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection' % guid)
try:
(pnp_id, ttype) = _winreg.QueryValueEx(connection_key, 'PnpInstanceID')
if ttype != _winreg.REG_SZ:
raise ValueError
device_key = _winreg.OpenKey(lm, 'SYSTEM\\CurrentControlSet\\Enum\\%s' % pnp_id)
try:
(flags, ttype) = _winreg.QueryValueEx(device_key, 'ConfigFlags')
if ttype != _winreg.REG_DWORD:
raise ValueError
return not (flags & 1)
finally:
device_key.Close()
finally:
connection_key.Close()
except (EnvironmentError, ValueError):
try:
(nte, ttype) = _winreg.QueryValueEx(interface_key, 'NTEContextList')
return nte is not None
except WindowsError:
return False
except:
None<EXCEPTION MATCH>WindowsError
None<EXCEPTION MATCH>WindowsError
def _compute_timeout(self, start):
now = time.time()
if now < start:
if start - now > 1:
raise Timeout
else:
now = start
duration = now - start
if duration >= self.lifetime:
raise Timeout
return min(self.lifetime - duration, self.timeout)
def query(self, qname, rdtype = dns.rdatatype.A, rdclass = dns.rdataclass.IN, tcp = False):
if isinstance(qname, (str, unicode)):
qname = dns.name.from_text(qname, None)
if isinstance(rdtype, str):
rdtype = dns.rdatatype.from_text(rdtype)
if isinstance(rdclass, str):
rdclass = dns.rdataclass.from_text(rdclass)
qnames_to_try = []
if qname.is_absolute():
qnames_to_try.append(qname)
elif len(qname) > 1:
qnames_to_try.append(qname.concatenate(dns.name.root))
if self.search:
for suffix in self.search:
qnames_to_try.append(qname.concatenate(suffix))
else:
qnames_to_try.append(qname.concatenate(self.domain))
all_nxdomain = True
start = time.time()
for qname in qnames_to_try:
if self.cache:
answer = self.cache.get((qname, rdtype, rdclass))
if answer:
return answer
request = dns.message.make_query(qname, rdtype, rdclass)
if self.keyname is not None:
request.use_tsig(self.keyring, self.keyname)
request.use_edns(self.edns, self.ednsflags, self.payload)
response = None
nameservers = self.nameservers[:]
backoff = 0.1
while response is None:
if len(nameservers) == 0:
raise NoNameservers
for nameserver in nameservers[:]:
timeout = self._compute_timeout(start)
try:
if tcp:
response = dns.query.tcp(request, nameserver, timeout, self.port)
else:
response = dns.query.udp(request, nameserver, timeout, self.port)
except (socket.error, dns.exception.Timeout):
response = None
continue
except dns.query.UnexpectedSource:
response = None
continue
except dns.exception.FormError:
nameservers.remove(nameserver)
response = None
continue
rcode = response.rcode()
if rcode == dns.rcode.NOERROR or rcode == dns.rcode.NXDOMAIN:
break
if rcode != dns.rcode.SERVFAIL:
nameservers.remove(nameserver)
response = None
if response is not None:
break
if len(nameservers) > 0:
timeout = self._compute_timeout(start)
sleep_time = min(timeout, backoff)
backoff *= 2
time.sleep(sleep_time)
continue
if response.rcode() == dns.rcode.NXDOMAIN:
continue
all_nxdomain = False
break
if all_nxdomain:
raise NXDOMAIN
answer = Answer(qname, rdtype, rdclass, response)
if self.cache:
self.cache.put((qname, rdtype, rdclass), answer)
return answer
def use_tsig(self, keyring, keyname = None):
self.keyring = keyring
if keyname is None:
self.keyname = self.keyring.keys()[0]
else:
self.keyname = keyname
def use_edns(self, edns, ednsflags, payload):
if edns is None:
edns = -1
self.edns = edns
self.ednsflags = ednsflags
self.payload = payload
default_resolver = None
def get_default_resolver():
global default_resolver
if default_resolver is None:
default_resolver = Resolver()
return default_resolver
def query(qname, rdtype = dns.rdatatype.A, rdclass = dns.rdataclass.IN, tcp = False):
return get_default_resolver().query(qname, rdtype, rdclass, tcp)
def zone_for_name(name, rdclass = dns.rdataclass.IN, tcp = False, resolver = None):
if isinstance(name, (str, unicode)):
name = dns.name.from_text(name, dns.name.root)
if resolver is None:
resolver = get_default_resolver()
if not name.is_absolute():
raise NotAbsolute, name
while None:
try:
answer = resolver.query(name, dns.rdatatype.SOA, rdclass, tcp)
return name
continue
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
try:
name = name.parent()
except dns.name.NoParent:
raise NoRootSOA
except:
None<EXCEPTION MATCH>dns.name.NoParent
None<EXCEPTION MATCH>dns.name.NoParent
return None